package com.wn.alibaba.cloud.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @FileName: DemoController
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/4/5 - 14:36
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@RestController
@RefreshScope
public class DemoController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/info")
    public String getInfo() {
        return configInfo;
    }

    @GetMapping("/testA")
    public String getTestA() {
        return "============Test-A";
    }

    @GetMapping("/testB")
    @SentinelResource("testB")
    public String getTestB() {
        return "============Test-B";
    }

    @GetMapping("/testC")
    public String getTestC() {
        // try {
        //     TimeUnit.SECONDS.sleep(1);
        // } catch (InterruptedException e) {
        //     e.printStackTrace();
        // }
        // log.info("test-C 测试RT");

        log.info("test-C 测试异常比例");
        int num = 10 / 0;
        return "============Test-C";
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey", blockHandler = "dealTestHotKey")
    public String getTestHotKey(@RequestParam(name = "p1", required = false) String p1,
                                @RequestParam(name = "p2", required = false) String p2) {
        log.info("testHotKey 测试热点流控");
        return "============testHotKey";
    }

    public String dealTestHotKey(@RequestParam(name = "p1", required = false) String p1,
                                 @RequestParam(name = "p2", required = false) String p2,
                                 BlockException ex) {
        log.info("testHotKey 测试热点流控", ex);
        return "============dealTestHotKey";
    }

    @GetMapping("/testHotKey1/{id}/{name}")
    @SentinelResource(value = "testHotKey1", blockHandler = "dealTestHotKey1")
    public String getTestHotKey1(@PathVariable("id") String id, @PathVariable("name") String name) {
        log.info("testHotKey 测试热点流控");
        return "============testHotKey1";
    }

    public String dealTestHotKey1(@PathVariable("id") String id,
                                  @PathVariable("name") String name,
                                  BlockException ex) {
        log.info("testHotKey 测试热点流控", ex);
        return "============dealTestHotKey1";
    }

    @PostMapping("/testHotKey2")
    @SentinelResource(value = "testHotKey2", blockHandler = "dealTestHotKey2")
    public String getTestHotKey2(@RequestBody Map<String, String> map) {
        log.info("testHotKey 测试热点流控");
        return "============testHotKey2";
    }

    public String dealTestHotKey2(@RequestBody Map<String, String> map,
                                  BlockException ex) {
        log.info("testHotKey 测试热点流控", ex);
        return "============dealTestHotKey2";
    }
}
